package com.epay;

import java.io.File;
import java.io.FileWriter;
import java.util.*;

public class CreaterDto {

	public static boolean createrDto(Map<String,Map<String,String>> map){
		try{
			String workpath = Constants.workpath;
			String tableNameLower=Constants.fileName;

			String tableName1=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1)+"ReqDTO";
			String tableName2=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1)+"ResDTO";
			String tableName3=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1)+"ListResDTO";
			//创建conttroller文件
			String dir1 = workpath + File.separator + CreaterUtil.replaceAll(Constants.struts_dto_pack, ".", File.separator)+"/request/";
			File dirfile1 = new File(dir1);
			if (!dirfile1.exists()) {
			   dirfile1.mkdirs();
			}
			String dir2 = workpath + File.separator + CreaterUtil.replaceAll(Constants.struts_dto_pack, ".", File.separator)+"/response/";
			File dirfile2 = new File(dir2);
			if (!dirfile2.exists()) {
				dirfile2.mkdirs();
			}
			String dir3 = workpath + File.separator + CreaterUtil.replaceAll(Constants.struts_dto_pack, ".", File.separator)+"/response/";
			File dirfile3 = new File(dir3);
			if (!dirfile3.exists()) {
				dirfile3.mkdirs();
			}

			File beanfile1 = new File(dir1 + tableName1+".java");
			FileWriter fw1 = new FileWriter(beanfile1);
			fw1.write("package " + Constants.struts_dto_pack + ".request;\n");
			fw1.write("import com.epay.common.request.RequestDTO;\n");
			fw1.write("\n");
			fw1.write("/** \n");
			fw1.write(" * DTO\n");
			fw1.write(" * @author epay\n");
			fw1.write(" * @date "+Constants.getSysDate()+"\n");
			fw1.write(" */\n");
			fw1.write("public class " + tableName1 + " extends RequestDTO  {");
			fw1.write("\n");

			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				String columnComment=pamentMap.get("columnComment");
				//根据数据库类型获取java类型     "java.lang.String"
				String type=CreaterUtil.getDbforJavaType(dataType);
				fw1.write("\t/**"+columnComment+"*/\n");
				fw1.write("\tprivate " + type + " " + name + ";\n");

			}

			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				String name1=String.valueOf(name);
				String name2=String.valueOf(name).substring(0,1).toUpperCase()+String.valueOf(name).substring(1);
				//根据数据库类型获取java类型     "java.lang.String"
				String type=CreaterUtil.getDbforJavaType(dataType);
				fw1.write("\n");
				fw1.write("\tpublic " + type + " get" + name2 + "(){\n");
				fw1.write("\t\treturn " + name1 + ";\n");
				fw1.write("\t}\n");
				fw1.write("\n");
				fw1.write("\tpublic void set" + name2 + "(" + type + " " + name1 + "){\n");
				fw1.write("\t\tthis." + name1 + "=" + name1 + ";\n");
				fw1.write("\t}\n");
			}
			fw1.write("}");
			fw1.close();


			File beanfile2 = new File(dir2 + tableName2+".java");
			FileWriter fw2 = new FileWriter(beanfile2);
			fw2.write("package " + Constants.struts_dto_pack + ".response;\n");
			fw2.write("import org.slf4j.Logger;\n");
			fw2.write("import org.slf4j.LoggerFactory;\n");
			fw2.write("import java.io.Serializable;\n");


			fw2.write("\n");
			fw2.write("/** \n");
			fw2.write(" * DTO\n");
			fw2.write(" * @author epay\n");
			fw2.write(" * @date "+Constants.getSysDate()+"\n");
			fw2.write(" */\n");
			fw2.write("public class " + tableName2 + " implements Serializable {");
			fw2.write("\n");
			fw2.write("private static final long serialVersionUID = 1L;\n");
			fw2.write("\tprivate static final Logger logger = LoggerFactory.getLogger("+tableName2+".class);\n");

			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				String columnComment=pamentMap.get("columnComment");
				//根据数据库类型获取java类型     "java.lang.String"
				String type=CreaterUtil.getDbforJavaType(dataType);

				fw2.write("\t/**"+columnComment+"*/\n");
				fw2.write("\tprivate " + type + " " + name + ";\n");

			}

			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				String name1=String.valueOf(name);
				String name2=String.valueOf(name).substring(0,1).toUpperCase()+String.valueOf(name).substring(1);
				//根据数据库类型获取java类型     "java.lang.String"
				String type=CreaterUtil.getDbforJavaType(dataType);
				fw2.write("\n");
				fw2.write("\tpublic " + type + " get" + name2 + "(){\n");
				fw2.write("\t\treturn " + name1 + ";\n");
				fw2.write("\t}\n");
				fw2.write("\n");
				fw2.write("\tpublic void set" + name2 + "(" + type + " " + name1 + "){\n");
				fw2.write("\t\tthis." + name1 + "=" + name1 + ";\n");
				fw2.write("\t}\n");
			}
			 fw2.write("}");
			 fw2.close();




			File beanfile3 = new File(dir3 + tableName3+".java");
			FileWriter fw3 = new FileWriter(beanfile3);
			fw3.write("import org.slf4j.Logger;\n");
			fw3.write("import org.slf4j.LoggerFactory;\n");
			fw3.write("import java.io.Serializable;\n");
			fw3.write("import com.epay.common.page.Page;\n");
			fw3.write("import java.util.List;\n");
			fw3.write("\n");
			fw3.write("/** \n");
			fw3.write(" * DTO\n");
			fw3.write(" * @author epay\n");
			fw3.write(" * @date "+Constants.getSysDate()+"\n");
			fw3.write(" */\n");
			fw3.write("public class " + tableName3 + " implements Serializable {");
			fw3.write("\n");
			fw3.write("private static final long serialVersionUID = 1L;\n");
			fw3.write("\tprivate static final Logger logger = LoggerFactory.getLogger("+tableName3+".class);\n");

			fw3.write("\t/**分页信息*/\n");
			fw3.write("\tprivate Page page;\n");
			fw3.write("\t/**数据信息*/\n");
			fw3.write("\tprivate List<"+tableName2+"> list;\n");


			fw3.write("\tpublic Page getPage() { return page; }\n");
			fw3.write("\tpublic void setPage(Page page) { this.page = page; \n}");
			fw3.write("\tpublic List<"+tableName2+"> getList() { return list; }\n");

			fw3.write("\tpublic void setList(List<"+tableName2+"> list) { this.list = list; }\n");






			fw3.write("}");
			fw3.close();

		}catch(Exception e){
			System.out.println("创建controller异常");
			e.printStackTrace();
		}
		return true;
	}
}
